/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.business.controller;

import com.shop.cereshop.business.param.shop.ShopCertificationFlowParam;
import com.shop.cereshop.business.page.shop.*;
import com.shop.cereshop.business.service.shop.ShopCertificationFlowService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.business.CerePlatformBusiness;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 店铺认证流程控制器
 */
@RestController
    @RequestMapping("certificationFlow")
@Api(tags = "店铺认证流程管理")
public class ShopCertificationFlowController {

    @Autowired
    private ShopCertificationFlowService shopCertificationFlowService;

    /**
     * OCR识别营业执照和身份证
     */
    @PostMapping("recognize")
    @ApiOperation(value = "OCR识别营业执照和身份证")
    public Result<RecognizeResp> recognize(@RequestBody @Valid ShopCertificationFlowParam param, HttpServletRequest request) throws CoBusinessException {
        try {
        return new Result<>(CoReturnFormat.SUCCESS, shopCertificationFlowService.recognize(param));
        } catch (Exception e) {
            Result<RecognizeResp> objectResult = new Result<>(CoReturnFormat.CODE_ERROR);
            objectResult.setMessage(e.getMessage());
            return  objectResult;
        }

    }

    /**
     * 开始认证流程
     */
    @PostMapping("start")
    @ApiOperation(value = "开始认证流程")
    public Result<StartFlowResp> start(@RequestBody @Valid ShopCertificationFlowParam param, HttpServletRequest request) throws CoBusinessException {
        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
        return new Result<>(CoReturnFormat.SUCCESS, shopCertificationFlowService.start(param, user));
    }

    /**
     * 人脸识别验证
     */
    @PostMapping("faceRecognition")
    @ApiOperation(value = "人脸识别验证")
    public Result<SimpleFlowResp> faceRecognition(@RequestBody @Valid ShopCertificationFlowParam param, HttpServletRequest request) throws CoBusinessException {
        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
        return new Result<>(CoReturnFormat.SUCCESS, shopCertificationFlowService.faceRecognition(param, user));
    }

    /**
     * 完成认证并创建店铺
     */
    @PostMapping("complete")
    @ApiOperation(value = "完成认证并创建店铺")
    public Result<CompleteResp> complete(@RequestBody @Valid ShopCertificationFlowParam param, HttpServletRequest request) throws CoBusinessException {
        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
        return new Result<>(CoReturnFormat.SUCCESS, shopCertificationFlowService.complete(param, user));
    }

    /**
     * 查询认证流程状态
     */
    @GetMapping("status/{flowId}")
    @ApiOperation(value = "查询认证流程状态")
    public Result<SimpleFlowResp> getStatus(@PathVariable String flowId, HttpServletRequest request) throws CoBusinessException {
        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
        return new Result<>(CoReturnFormat.SUCCESS, shopCertificationFlowService.getStatus(flowId, user));
    }

    /**
     * 取消认证流程
     */
    @PostMapping("cancel/{flowId}")
    @ApiOperation(value = "取消认证流程")
    public Result<SimpleFlowResp> cancel(@PathVariable String flowId, HttpServletRequest request) throws CoBusinessException {
        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
        return new Result<>(CoReturnFormat.SUCCESS, shopCertificationFlowService.cancel(flowId, user));
    }
}
